Skip to content

fix thread safety issue in client connection context.#2256

Merged
vwxyzh merged 1 commit into
Azure:devfrom
vwxyzh:z/bugfix2
Mar 11, 2026
Merged

fix thread safety issue in client connection context.#2256
vwxyzh merged 1 commit into
Azure:devfrom
vwxyzh:z/bugfix2

Conversation

@vwxyzh

@vwxyzh vwxyzh commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Summary of the changes

This pull request improves the handling and processing of partial messages in client connections, ensuring thread safety and correctness when dealing with fragmented messages. It also introduces a new test to verify that partial message processing is robust under concurrent scenarios.

Improvements to partial message processing:

  • Refactored the logic in ClientConnectionContext.cs to correctly accumulate and concatenate buffered partial messages before writing them to the application, ensuring that all fragments are combined in the correct order and thread safety is maintained. This includes switching from using long to int for buffer length calculation, and more carefully managing buffer memory and copy operations to avoid race conditions.

Testing enhancements:

  • Added a new test TestPartialMessagesProcessingShouldBeThreadSafe in ServiceConnectionTests.cs to simulate the reception of partial messages in a multi-iteration scenario, validating that the system correctly reconstructs full messages from fragments and remains thread-safe under load. This test also verifies correct connection and disconnection behavior.

Fixes #2253

@vwxyzh vwxyzh requested a review from vicancy March 11, 2026 03:12
@vwxyzh vwxyzh enabled auto-merge (squash) March 11, 2026 05:41
@vwxyzh vwxyzh merged commit f15f17e into Azure:dev Mar 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread-safety bug: Queue<T> _bufferedMessages in ClientConnectionContext accessed concurrently

2 participants